﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class Login : Form
    {
        TblCustomersDAO customerDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
        public Login()
        {
            InitializeComponent();
            txtEmail.Focus();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtEmail.Text != null || txtPass.Text != null)
                {
                    TblCustomers customer = customerDAO.GetCustomer(txtEmail.Text.Trim(), Encryption.EncryptToString(txtPass.Text));

                    if ((customer.Emailaddress == txtEmail.Text) && (customer.Emailpassword == Encryption.EncryptToString(txtPass.Text)) && (customer.IsAdmin == true))
                    {
                       // show noi dung ben trong khi login sucessful
                        Global.isloged = true;                    
                        flag = true;
                        this.Hide();
                      
                    }
                    else
                    {  
                        MessageBox.Show("Email invalid");
                    }
                }
                else
                {
                    MessageBox.Show("Email or Password not null");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (flag)
            {
               
            }
        }

        private void Login_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
        }

        private void Login_Enter(object sender, EventArgs e)
        {
            bool flag = false;
            try
            {
                if (txtEmail.Text != null || txtPass.Text != null)
                {
                    TblCustomers customer = customerDAO.GetCustomer(txtEmail.Text.Trim(), Encryption.EncryptToString(txtPass.Text));

                    if ((customer.Emailaddress == txtEmail.Text) && (customer.Emailpassword == Encryption.EncryptToString(txtPass.Text)) && (customer.IsAdmin == true))
                    {
                        // show noi dung ben trong khi login sucessful
                        Global.isloged = true;
                        flag = true;
                        this.Hide();

                    }
                    else
                    {
                        MessageBox.Show("Email invalid");
                    }
                }
                else
                {
                    MessageBox.Show("Email or Password not null");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (flag)
            {

            }
        }
    }
}
